package app.species;

import sim.portrayal.continuous.*;
import sim.engine.*;
import sim.display.*;

import javax.swing.*;
import java.awt.*;


public class SpeciesSimUI extends GUIState{

	private int displayWidth, displayHeight;
	private ConfigParams params;

	public Display2D display;
	public JFrame displayFrame;
	

	public BrainDisplay2D displayBrain;
	public JFrame displayBrainFrame;

	ContinuousPortrayal2D creaturePortrayal = new ContinuousPortrayal2D();
	ContinuousPortrayal2D stationaryFoodPortrayal = new ContinuousPortrayal2D();
	ContinuousPortrayal2D movingFoodPortrayal = new ContinuousPortrayal2D();
	
	ContinuousPortrayal2D predatorPortrayal = new ContinuousPortrayal2D();
	ContinuousPortrayal2D turretPortrayal = new ContinuousPortrayal2D();
	ContinuousPortrayal2D bulletPortrayal = new ContinuousPortrayal2D();

	public static void main(String[] args){

		startSimulation();

	}
	
	public static void startSimulation(){
		SpeciesSimUI simUI = new SpeciesSimUI();
		Console c = new Console(simUI);
		c.setVisible(true);
	}

	public SpeciesSimUI(SimState state){

		super(state);

		this.params = new ConfigParams("run.conf");

		this.displayWidth = this.params.getParam("DISPLAY_WIDTH",displayWidth);
		this.displayHeight = this.params.getParam("DISPLAY_HEIGHT",displayHeight);

	}

	public SpeciesSimUI(){

		this(new SpeciesSim( System.currentTimeMillis()));

	}

	public String getName() { return "NEATALS"; }
	
	public String getInfo() { return "<H2>Species</H2> Evolving competing species using NEAT."; }

	public Object getSimulationInspectedObject() { return state; }

	public void start(){

		super.start();
		setupPortrayals();

	}

	public void load(SimState state){

		super.load(state);
		setupPortrayals();

	}

	public void setupPortrayals(){

		SpeciesSim sim = (SpeciesSim) state;
		
		// tell the portrayals what to portray and how to portray them
		creaturePortrayal.setField(sim.creatures);
		stationaryFoodPortrayal.setField(sim.stationaryFood);
		movingFoodPortrayal.setField(sim.movingFood);
		predatorPortrayal.setField(sim.predators);
		turretPortrayal.setField(sim.turrets);
		bulletPortrayal.setField(sim.bullets);

		// reschedule the displayer
		display.reset();
		display.setBackdrop(Color.white);		
		displayBrain.reset();
		displayBrain.setBackdrop(Color.white);

		// redraw the display
		display.repaint();
		displayBrain.repaint();

	}

	public void init(Controller c){

		super.init(c);

		// make the displayer
		display = new Display2D(this.displayWidth,this.displayHeight,this,1);

		displayFrame = display.createFrame();
		displayFrame.setTitle("Species Environment");
		c.registerFrame(displayFrame);   // register the frame so it appears in the "Display" list
		displayFrame.setVisible(true);
		display.attach(this.bulletPortrayal, "Bullets");
		display.attach(this.turretPortrayal, "Turrets");
		display.attach(this.stationaryFoodPortrayal, "Stationary food");
		display.attach(this.movingFoodPortrayal, "Moving food");
		display.attach(this.predatorPortrayal, "Predators");
		display.attach(this.creaturePortrayal, "Creatures");
				
        // Make the Display2D.  We'll have it display stuff later.
		displayBrain = new BrainDisplay2D(400,400,this,1); // at 400x400, we've got 4x4 per array position
		displayBrainFrame = displayBrain.createFrame();
		displayBrain.attach(this.creaturePortrayal, "Creatures");
        c.registerFrame(displayBrainFrame);   // register the frame so it appears in the "Display" list
        
        

		
	}

	public void quit(){

		super.quit();

		if (displayFrame!=null) displayFrame.dispose();
		displayFrame = null;
		display = null;
		if (displayBrainFrame!=null) displayBrainFrame.dispose();
		displayBrainFrame = null;
		displayBrainFrame = null;

	}

}
